package com.jframe.sys.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarUtil {
	public final static int WEEK_TYPE_INT = 0; //整形数
	public final static int WEEK_TYPE_CN = 1; //中文
	public final static int WEEK_TYPE_EN = 2; //英文全
	public final static int WEEK_TYPE_EN_THUM = 3; //英文缩略
   static {

	}
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}
	
	public static String getDate(String strSdf) {
		Calendar RightNow = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(strSdf);
		String strcurDate = sdf.format(RightNow.getTime());
		return strcurDate;
	}
	public static String getDateTime() {

		return getDateTime("yyyy-MM-dd HH:mm:ss");
	}
	
	public static String getTime(){
		return getDateTime("HH:mm:ss");
	}
	
	public static int getHour(){
		String time = getTime();
		return StringUtil.parseInt(time.substring(0, 2));
	}
	
//	public static int getHour(String time){
//		
//	}
	/**
	 * 获取时间
	 * @param strSdf 时间格式 如：yyyy年MM月dd日
	 * @return
	 */
	public static String getDateTime(String strSdf) {
		Calendar RightNow = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(strSdf);
		String strcurDate = sdf.format(RightNow.getTime());
		return strcurDate;
	}
	
	public static Calendar getCalendar() {
		return Calendar.getInstance();
	}

	

	public static String getYearMonth() {
		return getYearMonth("yyyy-MM");
	}
	
	public static String getYearMonth(String sdf ) {
		return getDateTime(sdf);
	}

	public static int getYear() {
		String strcurDate = getDateTime("yyyy");
		return StringUtil.parseInt(strcurDate);
	}

	public static int getMonth() {
		return getMonth(getDate());
	}

	public static int getDay() {
		return getDay(getDate());
	}
    /**
     * 根据日期获取当前为周几
     * @param date
     * @return
     */
	public static int getWeek(String date){
		
		Calendar cal = Calendar.getInstance();
        cal.setTime( str2Date(date));
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)  w = 0;
        return w;
	}
	
	public static int getWeek(){
		return getWeek(getDate());
	}
	public static String getWeek(String date,int weekType) {
	        //String[] cnDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
	        String[] cnDays = {"日", "一", "二", "三", "四", "五", "六"};
	        String[] enDays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday"};
	        String[] enJDays = {"SUN", "MON", "TUE", "WED", "THU", "FRI","SAT"};
	        
	        int w = getWeek(date);
	        if( weekType== CalendarUtil.WEEK_TYPE_CN ) { return cnDays[w];}
	        else if (weekType== CalendarUtil.WEEK_TYPE_EN) { return enDays[w];}
	        else if (weekType== CalendarUtil.WEEK_TYPE_EN_THUM){return enJDays[w];}
	        else return w+ "" ;
	        
	}
	
	public static String getCnWeek(){
		
		return getCnWeek(getDate());
	}
	
   public static String getCnWeek(String date){
		
		return getWeek(date,CalendarUtil.WEEK_TYPE_CN);
	}
	

	public static String cnDate2twDate(String strDate) {
		if (strDate == null || strDate.equals("")) {
			return "";
		}
		StringUtil.replace(strDate, "-", "/");
		int pos = strDate.indexOf("/");
		if (pos < 0) {
			pos = strDate.indexOf("-");
		}
		if (pos < 0) {
			return strDate;
		}
		int year = Integer.parseInt(strDate.substring(0, pos));
		if (year < 49) {
			year += 2000;
		}
		if (year > 49 && year < 1911) {
			year += 1900;
		}
		return String.valueOf(year - 1911)
				+ strDate.substring(pos, strDate.length());
	}

	public static String twDate2cnDate(String strDate) {
		if (strDate == null || strDate.equals("")) {
			return "";
		}
		StringUtil.replace(strDate, "-", "/");
		int pos = strDate.indexOf("/");
		if (pos < 0) {
			return strDate;
		}
		int year = Integer.parseInt(strDate.substring(0, pos));
		if (year < 1000) {
			year += 1911;
		}
		return String.valueOf(year) + strDate.substring(pos, strDate.length());
	}
	public static java.util.Date str2Date(String str)
	{
		return str2Date(str,"yyyy-MM-dd");
	}
	public static java.util.Date str2Date(String sourceStr, String formatString) {
		try {
			java.text.SimpleDateFormat f = new java.text.SimpleDateFormat(
					formatString);
			return f.parse(sourceStr);
		} catch (Exception e) {
			return null;
		}
	}

	public static java.sql.Date str2DBDate(String sourceStr) {
		try {
			sourceStr = twDate2cnDate(sourceStr);
			String formatString = "yyyy/MM/dd HH:mm:ss";
			if (sourceStr.length() == 10) {
				formatString = "yyyy/MM/dd";
			}
			java.text.SimpleDateFormat f = new java.text.SimpleDateFormat(
					formatString);
			return (new java.sql.Date(f.parse(sourceStr).getTime()));
		} catch (Exception e) {
			return null;
		}
	}

	public static String dbDate2Str(java.sql.Date d1) {
		try {
			if (d1 == null) {
				return "";
			}
			String formatString = "yyyy/MM/dd";
			// sql.date--->util.date--->��Ԫ���ַ�--->����ַ�
			return cnDate2twDate(date2Str(new java.util.Date(d1.getTime()),
					formatString));
		} catch (Exception e) {
			return "";
		}
	}

	public static String dbDate2StrDatetime(java.sql.Date d1) {
		try {
			if (d1 == null) {
				return "";
			}
			String formatString = "yyyy/MM/dd HH:mm:ss";
			// sql.date--->util.date--->��Ԫ���ַ�--->����ַ�
			return cnDate2twDate(date2Str(new java.util.Date(d1.getTime()),
					formatString));
		} catch (Exception e) {
			return "";
		}
	}
	public static String date2Str(java.util.Date date) {
		return date2Str(date,"yyyy-MM-dd");
	}
	
	public static String date2Str(java.util.Date date, String sdf) {
		try {
			java.text.SimpleDateFormat f = new java.text.SimpleDateFormat(
					sdf);
			return f.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	public static int CompDate(String strDate1, String strDate2) {
		long time1, time2;
		java.util.Date Date1 = null, Date2 = null;
		if (strDate1 == null || strDate1 == "") {
			time1 = 0;
		} else {
			Date1 = str2Date(strDate1, "yyyy/MM/dd hh:mm:ss");
			if (Date1 == null) {
				Date1 = str2Date(strDate1, "yyyy/MM/dd");
			}
			if (Date1 == null) {
				time1 = 0;
			} else {
				time1 = Date1.getTime();
			}
		}

		if (strDate2 == null || strDate2 == "") {
			time2 = 0;
		} else {
			Date2 = str2Date(strDate2, "yyyy/MM/dd hh:mm:ss");
			if (Date2 == null) {
				Date2 = str2Date(strDate2, "yyyy/MM/dd");
			}
			if (Date2 == null) {
				time2 = 0;
			} else {
				time2 = Date2.getTime();
			}
		}
		if (time1 < time2) {
			return -1;
		}
		if (time1 > time2) {
			return 1;
		}
		return 0;
	}

	/**
	 * 日期相减，得到间隔天数
	 * 
	 * @param date1
	 *            String
	 * @param date2
	 *            String
	 * @return int 返回间隔天数
	 */
	public static long diffDate(String date1, String date2) {
		try {
			long day = 24 * 3600 * 1000; //
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			java.util.Date Date1 = sdf.parse(date1);
			java.util.Date Date2 = sdf.parse(date2);
			long diffTime = Date1.getTime() - Date2.getTime();
			long diffDate =  diffTime / day;
			return diffDate;

		} catch (Exception ex) {
			return -1;
		}

	}
    
	public static long minusDate(String date1,String date2){
		try {
			long day = 24 * 3600 * 1000; //
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			if(date1.length() <= 10 ) date1 = date1 + " 00:00:00";
			if(date2.length() <= 10 ) date2 = date2 + " 00:00:00";
			java.util.Date Date1 = sdf.parse(date1);
			java.util.Date Date2 = sdf.parse(date2);
			long diffTime = Date1.getTime() - Date2.getTime();
			return diffTime;
		} catch (Exception ex) {
			return -1;
		}
	}
	
	public static boolean compare(String date1,String date2){
		long l = diffDate(date1,date2); 
	    return l>=0 ?  true :false;
	}
	/**
	 * 如果指定日期大于当前日期days天数则为ture
	 * @param date
	 * @param days
	 * @return
	 */
	public static boolean compare(String date,int days){
		long l = diffDate(date,getDate());
	    return l>=days ?  true :false;
	}
	
	public static boolean equals(String date1,String date2){
		
		long l = diffDate(date1,date2); 
		return l==0 ? true:false;
	}
	public static int getYear(String sDate) {
		try {
			String sign = "/";
			
			int pos1 = sDate.indexOf(sign);
			if (pos1 == -1) {
				sign = "-";
			}
			pos1 = sDate.indexOf(sign);
			if (pos1 == -1) {
				if (sDate.length() < 4 || sDate.compareTo("1900") < 0) {
					return -1;
				}
			}
			pos1 = 4;
			return StringUtil.getNumToInt(sDate.substring(0, pos1));
		} catch (Exception ex) {
			System.out.println("error in methor getYear():" + ex);
			return -1;
		}
	}

	public static int getCurrentYear() {
		try {
			int currentYear = 1900;
			String temp = "";
			temp = getDate().substring(0, 4);
			currentYear = Integer.parseInt(temp);
			return currentYear;
		} catch (Exception ex) {
			System.out.println("error in methor getCurrentYear():" + ex);
			return -1;
		}
	}

	public static int getMonth(String sDate) {
		try {
			String sign = "/";
			int pos1 = sDate.indexOf(sign);
			if (pos1 == -1) {
				sign = "-";
			}
			String strArray[] = StringUtil.strToArray(sDate, sign);
			
			return StringUtil.parseInt(  strArray[1]);
		} catch (Exception ex) {
			System.out.println("error in methor getMonth():" + ex);
			return -1;
		}
	}

	public static int getDay(String sDate) {
		try {
			sDate = getDateStr(sDate);
			String sign = "/";
			int pos1 = sDate.lastIndexOf(sign);
			if (pos1 == -1) {
				sign = "-";
			}
			pos1 = sDate.lastIndexOf(sign);
			if (pos1 == -1) {
				return -1;
			}
			return StringUtil.getNumToInt(sDate.substring(pos1 + 1, sDate
					.length()));
		} catch (Exception ex) {
			System.out.println("error in methor getDay():" + ex);
			return -1;
		}
	}

	public static String getDateStr(String sDateTime) {
		if (sDateTime.indexOf(" ") == 10 && sDateTime.indexOf(":") > 11) {
			sDateTime = sDateTime.substring(0, sDateTime.indexOf(" "));
		}
		return sDateTime;
	}

	public static String getTimeStr(String sDateTime) {
		if (sDateTime.indexOf(" ") == 10 && sDateTime.indexOf(":") > 11) {
			sDateTime = sDateTime.substring(sDateTime.indexOf(" ") + 1,
					sDateTime.length());
		} else {
			sDateTime = "";
		}
		return sDateTime;
	}

	public static String str2Date2Str(String sDate, String sFormatDate) {
		try {
			return date2Str(str2Date(sDate, sFormatDate), sFormatDate);
		} catch (Exception ex) {
			System.out.println("error in methor str2Date2Str()" + ex);
			return "error:参数错误," + ex;
		}
	}

	
	public static String getChangeDate(String sDate, String sYMD, int nums) {
		try {
			if(nums==0) return sDate;
			if (sYMD == null
					|| (!"Y".equalsIgnoreCase(sYMD)
							&& !"M".equalsIgnoreCase(sYMD) && !"D"
							.equalsIgnoreCase(sYMD))) {
				return "error:参数sYMD格式错误!";
			}
			String sign = "/";
			int pos1 = sDate.lastIndexOf(sign);
			if (pos1 == -1) {
				sign = "-";
			}
			String sTime = getTimeStr(sDate);
			sDate = getDateStr(sDate);
			int year = getYear(sDate);
			int month = getMonth(sDate);
			int day = getDay(sDate);
			if (year == -1 || month == -1 || day == -1) {
				return "error:(" + sDate + ")";
			}
			if ("Y".equalsIgnoreCase(sYMD)) {
				year += nums;
			}
			if ("M".equalsIgnoreCase(sYMD)) {
				month += nums;
			}
			if ("D".equalsIgnoreCase(sYMD)) {
				day += nums;
			}
			Calendar calendar = Calendar.getInstance();
			calendar.set(year, month - 1, day);
			month = calendar.get(Calendar.MONTH) + 1;
			day = calendar.get(Calendar.DAY_OF_MONTH);
			year = calendar.get(Calendar.YEAR);
			String sRet = year + sign + (month < 10 ? "0" : "") + month + sign
					+ (day < 10 ? "0" : "") + day;
			if (!sTime.equals("")) {
				sRet += " " + sTime;
			}
			return sRet;
		} catch (Exception ex) {
			System.out.println("error in methor getChangeDate()" + ex);
			return "error：" + ex;
		}
	}

	public static String getChangeDate2(String sDate, String sYMD, int nums) {
		try {
			if (sYMD == null
					|| (!"Y".equalsIgnoreCase(sYMD)
							&& !"M".equalsIgnoreCase(sYMD) && !"D"
							.equalsIgnoreCase(sYMD))) {
				return "error:参数sYMD错误!";
			}
			String sign = "/";
			int pos1 = sDate.lastIndexOf(sign);
			if (pos1 == -1) {
				sign = "-";
			}
			String sTime = getTimeStr(sDate);
			sDate = getDateStr(sDate);
			int year = getYear(sDate);
			int month = getMonth(sDate);
			int day = getDay(sDate);
			if (year == -1 || month == -1 || day == -1) {
				return "error:(" + sDate + ")";
			}
			if ("Y".equalsIgnoreCase(sYMD)) {
				year -= nums;
			}
			if ("M".equalsIgnoreCase(sYMD)) {
				month -= nums;
			}
			if ("D".equalsIgnoreCase(sYMD)) {
				day -= nums;
			}
			Calendar calendar = Calendar.getInstance();
			calendar.set(year, month - 1, day);
			month = calendar.get(Calendar.MONTH) + 1;
			day = calendar.get(Calendar.DAY_OF_MONTH);
			year = calendar.get(Calendar.YEAR);
			String sRet = year + sign + (month < 10 ? "0" : "") + month + sign
					+ (day < 10 ? "0" : "") + day;
			if (!sTime.equals("")) {
				sRet += " " + sTime;
			}
			return sRet;
		} catch (Exception ex) {
			System.out.println("error in methor getChangeDate()" + ex);
			return "error:" + ex;
		}
	}

	public static String changeDateSign(String sDate, String sOldSign,
			String sNewSign) {
		if (sDate == null || "".equals(sDate)) {
			return sDate;
		}
		return StringUtil.replace(sDate, sOldSign, sNewSign);
	}

	public static int ageToBirthYear(int age) {
		int CurrentYear = getYear();		
		return CurrentYear - age;
	}

	public static int birthday2Age(String birthday) {
		try {
			if (birthday == null || birthday.equals("")) {
				return 0;
			}
			int thisYear = getYear();
			int birthYear = getYear(birthday);
			if (birthYear < 1) {
				return 0;
			}
			int age = thisYear - birthYear;
			return age;
		} catch (Exception ex) {
			return 0;
		}
	}
/*
	public static String formateDate(String date) {
		date = StringUtil.replace(StringUtil.replace(date, "-", ""), "/",
				"");
		if (!StringUtil.checkNum(date))
			return "";
		if (date.length() < 4)
			return date;
		if (date.length() == 4) {
			return date + "0101";
		} else if (date.length() == 5) {
			char c = date.charAt(4);
			if (c != '0') {
				return date.substring(0, 4) + "0" + c + "01";
			} else {
				return date + "101";
			}
		} else if (date.length() == 6) {
			int d = StringUtil.parseInt(date.substring(4, 6));
			if (d > 12) {
				return date.substring(0, 4) + "0101";
			} else {
				return date + "01";
			}
		} else if (date.length() == 7) {
			String date2 = "";
			int d = StringUtil.parseInt(date.substring(4, 6));
			int d2 = StringUtil.parseInt(date.substring(6, 7));
			if (d > 12) {
				date2 = date.substring(0, 4) + "01";
			} else {
				date2 = date.substring(0, 6);
			}
			if (d2 != 0) {
				date2 += "0" + d2;
			} else {
				date2 += "01";
			}
			return date2;
		} else if (date.length() >= 8) {
			date = date.substring(0, 8);
			String date2 = "";
			int d = StringUtil.parseInt(date.substring(4, 6));
			int d2 = StringUtil.parseInt(date.substring(6, 8));
			if (d > 12) {
				date2 = date.substring(0, 4) + "01";
			} else {
				date2 = date.substring(0, 6);
			}
			if (d2 > 31) {
				date2 += "01";
			} else if (d == 2 && d2 > 29) {
				date2 += "01";
			} else if ((d == 4 || d == 6 || d == 9 || d == 11) && d > 30) {
				date2 += "01";
			} else {
				date2 = date;
			}
			return date2;

		}

		return date;

	}
*/
	public static boolean isLeapYear(int year) {
		if (0 == year % 4 && (year % 100 != 0 || year % 400 == 0)) {
			return true;
		} else {
			return false;
		}

	}
	
   /**
    * 获取指定月份有多少天
    * @param year
    * @param month
    * @return
    */
	public static int getMonthDays(int year, int month) {
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			return 31;
		} else if (month == 2) {
			if (isLeapYear(year)) {
				return 29;
			} else {
				return 28;
			}
		} else {
			return 30;
		}

	}
	
	public static int getMonthDays(String date){
		int year = getYear(date);
		int month = getMonth(date);
		return getMonthDays(year,month);
	}
	
	public static String formateDate(String date){
		if(date==null || "".equals(date)){
			return "";
		}
		date = StringUtil.replaceAll(date, "/", "-");
		String a[] = StringUtil.split(date,"-");
		int year = StringUtil.parseInt(a[0]);
		
		String newDate= "";// + year ; //beginYear + year;
		if(year<10){
			newDate +="0" + year;
		}
		else {
			newDate += year;
		}
		int curYear = getYear()-2000; 
		if(year <=40 && year <100){ 
			newDate= "20" + newDate;
		}
		else if( year >40 && year <100){
			newDate= "19" + newDate;
		}
		if(a.length>1){
			int m = StringUtil.parseInt(a[1]);
			if(m<10){
				newDate = newDate +"-0"+m;
			}
			else{
				newDate = newDate +"-" +m;
			}
			if(a.length>2){
				int d = StringUtil.parseInt(a[2]);
				if(d<10){
					newDate = newDate +"-0"+d;
				}
				else{
					newDate = newDate +"-" +d;	
				}
			}
		}
		return newDate;
	}
	 public static String addDateTime(String day, int x) 
	    { 
	        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制 
	        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//12小时制 
	        Date date = null; 
	        try 
	        { 
	            date = format.parse(day); 
	        } 
	        catch (Exception ex)    
	        { 
	            ex.printStackTrace(); 
	        } 
	        if (date == null) return ""; 
	        Calendar cal = Calendar.getInstance(); 
	        cal.setTime(date); 
	        cal.add(Calendar.HOUR_OF_DAY, x);//24小时制 
	        //cal.add(Calendar.HOUR, x);12小时制 
	        date = cal.getTime(); 
	        System.out.println("front:" + date); 
	        cal = null; 
	        return format.format(date); 
	    } 

	public static void main(String args[]) {
		//System.out.println(ExtendCalendar.isBissextile(2012));
//		long l = ExtendCalendar.minusDate( 
//				 ExtendCalendar.getChangeDate(ExtendCalendar.getDate(), "D", 1) , ExtendCalendar.getDateTime());
//		l = l/(1000 * 60 * 60);
//		System.out.println(l);
		
		System.out.println(formateDate("2013/8/10"));
		
	}
}
